package com.yonyou.hxjy.config;

import cn.hutool.json.JSONNull;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.fasterxml.jackson.databind.module.SimpleModule;

import java.io.IOException;

/**
 * @author lhj
 */
@Configuration
public class JacksonConfig {

    @Bean
    public ObjectMapper objectMapper() {
        ObjectMapper mapper = new ObjectMapper();
        // 禁用空对象检查
        mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);

        // 注册自定义序列化器：将 JSONNull 转为 null
        SimpleModule module = new SimpleModule();
        module.addSerializer(JSONNull.class, new JsonSerializer<JSONNull>() {
            @Override
            public void serialize(JSONNull value, JsonGenerator gen, SerializerProvider provider) throws IOException {
                gen.writeNull(); // 直接输出为 null
            }
        });
        mapper.registerModule(module);

        return mapper;
    }
}